﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Bierdopje.API;
using Sabnzbd;

namespace Sub
{
    public partial class Wizard : Form
    {
        public ApiShow _Show;

        public Wizard(ApiShow show)
        {
            InitializeComponent();

            _Show = show;
            apiShowBindingSource.DataSource = _Show;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                tbLocation.Text = folderBrowserDialog1.SelectedPath;
                btnNext.Enabled = true;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (pBrowse.Visible)
            {
                pBrowse.Visible = false;
                pShowName.Visible = true;
                btnNext.Enabled = true;
                btnPrevious.Enabled = true;
            }
            else if (pShowName.Visible)
            {
                pShowName.Visible = false;
                pFilter.Visible = true;
            }
            else if (pFilter.Visible)
            {
                pFilter.Visible = false;
                pRename.Visible = true;
            }
            else if (pRename.Visible)
            {
                pRename.Visible = false;
                pSettings.Visible = true;
                btnNext.Text = "&Fetch";
            }
            else if (pSettings.Visible)
            {
                btnNext.Enabled = false;
                btnPrevious.Enabled = false;
                btnCancel.Enabled = false;
                try
                {
                    FetchSubs();
                }
                finally
                {
                    btnCancel.Enabled = true;
                }
            }
        }

        private void FetchSubs()
        {
            // Build an episode list for the selected seasons
            List<ApiEpisode> _EpisodeList = _Show.GetAllEpisodesForSeason(FetchSeasons());

            // Iterate each episode
            foreach (ApiEpisode _Episode in _EpisodeList)
            {
                // Check if the ep has subs
                if (_Episode.NLSubPresent)
                {
                    // Get a list of NL subs and download those (use appr. filter)
                    if (!DownloadSubs(_Episode, _Episode.GetNLSubs()))
                    { // No subs matched the filter
                        // Check for ENG subs
                        if ((cbAlsoEnglish.Checked) && (_Episode.ENGSubPresent))
                        {
                            // Download the ENG subs
                            DownloadSubs(_Episode, _Episode.GetENGSubs());
                        }
                    }
                }
                else
                {
                    if ((cbAlsoEnglish.Checked) && (_Episode.ENGSubPresent))
                    {
                        // Download the ENG subs
                        DownloadSubs(_Episode, _Episode.GetENGSubs());
                    }
                }
            }

            Close();
        }

        private bool DownloadSubs(ApiEpisode episode, List<ApiSub> subtitleList)
        {
            bool downloadedSub = false;
            foreach (ApiSub _Sub in subtitleList)
            {
                if ((cbFilter.Text.Equals("")) || (TextHelper.WildcardTextCompare(cbFilter.Text, _Sub.FileName, false)))
                {
                    string subFileName = "";
                    if (cbRemoveSubVersion.Checked)
                    {
                        subFileName = _Sub.FileNameWithoutVersion;
                    }
                    else
                    {
                        subFileName = _Sub.FileName;
                    }

                    if (!subFileName.EndsWith(".srt"))
                    {
                        subFileName += ".srt";
                    }
                    string newFileName = SABnzbd.CreateSabnzbdFileName(tbLocation.Text, cbRename.Text, _Show.Name, episode.Season, episode.Episode, episode.Title, subFileName);

                    Directory.CreateDirectory(Path.GetDirectoryName(newFileName));

                    _Sub.Download(newFileName, true);
                    downloadedSub = true;
                }
            }
            return downloadedSub;
        }

        private List<int> FetchSeasons()
        {
            List<int> Seasons = new List<int>();
            if (cbAllSeasons.Checked)
            {
                for (int i = 0; i < _Show.Seasons; i++)
                {
                    Seasons.Add(i+1);
                }
            }
            else
            {
                string[] s = tbSeasonList.Text.Split(',');
                foreach (string season in s)
                {
                    try
                    {
                        Seasons.Add(Convert.ToInt32(season.Trim()));
                    }
                    catch { }
                }
            }

            return Seasons;
        }

        private void cbAllSeasons_CheckedChanged(object sender, EventArgs e)
        {
            tbSeasonList.Enabled = !cbAllSeasons.Checked;
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (pBrowse.Visible)
            {
            }
            else if (pShowName.Visible)
            {
                pBrowse.Visible = true;
                pShowName.Visible = false;
                btnPrevious.Enabled = false;
            }
            else if (pFilter.Visible)
            {
                pFilter.Visible = false;
                pShowName.Visible = true;
            }
            else if (pRename.Visible)
            {
                pFilter.Visible = true;
                pRename.Visible = false;
            }
            else if (pSettings.Visible)
            {
                pSettings.Visible = false;
                pRename.Visible = true;
                btnNext.Text = "&Next";
                btnNext.Enabled = true;
                btnPrevious.Enabled = true;
                btnCancel.Enabled = true;
            }
        }
    }
}
